#include "sha.h"
#include <QtGui>
sha::sha(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	//ui.setupUi(this);
	QFrame * fra = new QFrame;
	QVBoxLayout * vbl = new QVBoxLayout;
	QPushButton * pb = new QPushButton("Get Shared Memory");
	vbl->addWidget(pb);

	lab = new QLabel("whatthefk");
	vbl->addWidget(lab);

	fra->setLayout(vbl);
	//this->setLayout(vbl);
	connect(pb,SIGNAL(clicked()),this,SLOT(OnBtn()));
	setCentralWidget(fra);
}

sha::~sha()
{

}

void sha::OnBtn()
{
	QSharedMemory sharedMemory("whatthefk");
	if (!sharedMemory.attach()) {
		lab->setText(tr("Unable to attach to shared memory segment.\n" \
			"Load an image first."));
		return;
	}

	QBuffer buffer;
	QDataStream in(&buffer);
	QImage image;

	sharedMemory.lock();
	buffer.setData((char*)sharedMemory.constData(), sharedMemory.size());
	buffer.open(QBuffer::ReadOnly);
	in >> image;
	sharedMemory.unlock();

	sharedMemory.detach();
	lab->setPixmap(QPixmap::fromImage(image));
}
